package cn.wolfcode.luowowo.website.interceptor;

import cn.wolfcode.luowowo.cache.service.IUserInfoCacheService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.common.util.Consts;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import com.alibaba.fastjson.JSON;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
public class CheckLoginInterceptor extends HandlerInterceptorAdapter {


    @Reference
    private IUserInfoCacheService userInfoCacheService;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        //1.判断动态资源
        if (handler instanceof HandlerMethod) {
            //2.判断本地session是否有对象
            HttpSession session = request.getSession();
            Object userInfo = session.getAttribute(Consts.USER_INFO);
            if (userInfo != null) {
                return true;
            }
            //3.本地session没有,就查询远程redis
            Cookie[] cookies = request.getCookies();
            if (cookies != null) {//当客户端中有cookie
                for (Cookie cookie : cookies) {
                    if (cookie.getName().equals(Consts.USER_INFO_TOKEN)) {//判断此token在redis中是否还存在
                        String token = cookie.getValue();//获取客户端中cookie缓存的token
                        //4.在redis中查询到json字符串
                        String json = userInfoCacheService.getUserInfo(token);
                        //5.把json字符串转换成UserInfo对象
                        UserInfo parseObject = JSON.parseObject(json, UserInfo.class);
                        //6.存入本地session
                        session.setAttribute(Consts.USER_INFO, parseObject);
                    }
                }
            }

            //7.判断是否有登录检查注解
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            if (!handlerMethod.hasMethodAnnotation(RequireLogin.class)) {
                return true;
            }

            //没有登录
            if (handlerMethod.hasMethodAnnotation(ResponseBody.class)) {//如果是ajax请求
                AjaxResult result = new AjaxResult();
                result.mark("请先登录");
                result.setCode(AjaxResult.NO_LOGIN_CODE);
                response.setContentType("text/json;charset=UTF-8");
                response.getWriter().print(JSON.toJSONString(result));
            } else {//如果是普通请求
                //8.访问的资源需要登录,而检查到用户没有登录就直接返回,登录页面
                response.sendRedirect("/login.html");
            }
            return false;
        }
        return true;//静态资源就直接放行
    }
}
